package com.cybertron.am.wordpress.util;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;

public class ProxyInfo implements Serializable {

    public enum Type {
        PUBLIC, PRIVATE;

        public static Type val(String type) {
            if (StringUtils.isBlank(type)) {
                return null;
            } else if (StringUtils.startsWithIgnoreCase(type, "PUBLIC")) {
                return PUBLIC;
            } else if (StringUtils.startsWithIgnoreCase(type, "PRIVATE")) {
                return PRIVATE;
            }
            throw new IllegalArgumentException("could not find " + type);
        }
    }

    public enum Protocol {
        HTTP, SOCKS;

        public static Protocol val(String protocol) {
            if (StringUtils.isBlank(protocol)) {
                return null;
            } else if (StringUtils.startsWithIgnoreCase(protocol, "socks")) {
                return SOCKS;
            } else if (StringUtils.startsWithIgnoreCase(protocol, "http")) {
                return HTTP;
            }
            throw new IllegalArgumentException("could not find " + protocol);
        }
    }

    public ProxyInfo() {
    }

    public ProxyInfo(String ip, Integer port, Protocol protocol) {
        this.ip = ip;
        this.port = port;
        this.protocol = protocol;
    }

    public ProxyInfo(String ip, Integer port, String protocol, Type type, String from) {
        this.ip = ip;
        this.port = port;
        this.protocol = Protocol.val(protocol);
        this.type = type;
        this.from = from;
    }

    public ProxyInfo(String ip, Integer port, Protocol protocol, Type type, String from) {
        this.ip = ip;
        this.port = port;
        this.protocol = protocol;
        this.type = type;
        this.from = from;
    }

    private String ip;

    private Integer port;

    private Protocol protocol;

    /**
     * 1 PUBLIC IP 2 PRIVATE IP
     */
    private Type type;

    private String from;

    private String username;

    private String password;

    private Date usedTime;

    private Date createdTime;

    private static final long serialVersionUID = 1L;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip == null ? null : ip.trim();
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Date getUsedTime() {
        return usedTime;
    }

    public void setUsedTime(Date usedTime) {
        this.usedTime = usedTime;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public Protocol getProtocol() {
        return protocol;
    }

    public void setProtocol(Protocol protocol) {
        this.protocol = protocol;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ProxyInfo proxyInfo = (ProxyInfo) o;

        if (!ip.equals(proxyInfo.ip)) return false;
        if (!port.equals(proxyInfo.port)) return false;
        if (protocol != proxyInfo.protocol) return false;
        return type == proxyInfo.type;
    }

    @Override
    public int hashCode() {
        int result = ip.hashCode();
        result = 31 * result + port.hashCode();
        result = 31 * result + protocol.hashCode();
        result = 31 * result + type.hashCode();
        return result;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "ProxyInfo{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                ", protocol=" + protocol +
                ", type=" + type +
                ", from='" + from + '\'' +
                '}';
    }
}
